// pages/player/player.js
let musiclist = []
// 正在播放歌曲的index
let nowPlayingIndex = 0
// 获取全局唯一的背景音频管理器
const backgroundAudioManager = wx.getBackgroundAudioManager()
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */

  data: {
    picUrl: '',
    isPlaying: false, // false表示不播放，true表示正在播放
    isLyricShow: false, //表示当前歌词是否显示
    lyric: '',
    isSame: false, // 表示是否为同一首歌
    musiclist:'',//用户点击播放的歌曲
    musicInfo:{} ,//当前歌曲信息
    musicAuter:{},//歌手
    index:2,
    historyList:[
      {
        name:'爱笑的眼睛',
        url:'https://7773-wsm849923370-2g83ffckce0a732a-1305059282.tcb.qcloud.la/music/%E7%88%B1%E7%AC%91%E7%9A%84%E7%9C%BC%E7%9D%9B.mp3?sign=bae3b555953b7a463189b5a373f27935&t=1617953484'
    },
    {
      name:'她说',
      url:'https://7773-wsm849923370-2g83ffckce0a732a-1305059282.tcb.qcloud.la/music/%E5%A5%B9%E8%AF%B4.mp3?sign=680e51194181e67d5f445c02aedb8241&t=1617953467'
  },
  {
    name:'江南',
    url:'https://7773-wsm849923370-2g83ffckce0a732a-1305059282.tcb.qcloud.la/music/%E6%B1%9F%E5%8D%97.mp3?sign=2c3bdfe00d4c47c0cdf0c405495ca4b3&t=1617953452'
}, {
        name:'不为谁而作的歌',
        url:'https://7773-wsm849923370-2g83ffckce0a732a-1305059282.tcb.qcloud.la/music/%E4%B8%8D%E4%B8%BA%E8%B0%81%E8%80%8C%E4%BD%9C%E7%9A%84%E6%AD%8C.mp3?sign=d4a40169dac989456e6e1c294797bf17&t=1617953435'
    },
    {
      name:'美人鱼',
      url:'https://7773-wsm849923370-2g83ffckce0a732a-1305059282.tcb.qcloud.la/music/%E7%BE%8E%E4%BA%BA%E9%B1%BC.mp3?sign=0e2a546a3d9c63a7e5f084c981d00683&t=1617953368'
  }
  ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    musiclist = wx.getStorageSync('musicname')
   this.setData({
    musiclist
   })
   wx.setNavigationBarTitle({
    title: musiclist  //页面标题
   })

   const db = wx.cloud.database()
   db.collection('musicer').where({
      name:musiclist
   }).get().then(res=>{
     const musicInfo =res.data
   this.setData({
    musicInfo:musicInfo[0]
   })
     backgroundAudioManager.src=musicInfo[0].musicUrl ||'https://7773-wsm849923370-2g83ffckce0a732a-1305059282.tcb.qcloud.la/music/%E7%BE%8E%E4%BA%BA%E9%B1%BC.mp3?sign=0e2a546a3d9c63a7e5f084c981d00683&t=1617953368'
     backgroundAudioManager.title=musiclist ||'美人鱼'
   })
   const DB = wx.cloud.database().collection('userHistory');
   let musicInfo =this.data.musicInfo
   if(musicInfo.auter){
    DB.add({
      data:{
        userName:'王帅鸣',
        auter:musicInfo.auter,
       musicUrl:musicInfo.musicUrl,
       name:musicInfo.name
      }
    })
   }
  
  },


  togglePlaying() {
    // 正在播放
    if (this.data.isPlaying) {
      backgroundAudioManager.pause()
    } else {
      backgroundAudioManager.play()
    }
    this.setData({
      isPlaying: !this.data.isPlaying
    })
  },

  onPrev() {
if(this.data.index!==0){
    let index=this.data.index
   this.setData({
     index:index-1
   })
    backgroundAudioManager.src=this.data.historyList[this.data.index].url
    backgroundAudioManager.title=this.data.historyList[this.data.index].name
  }else{
    wx.showToast({
      title: '当前是第一首',
      icon:'none'
    })
  }
  },
  onNext() {
if(this.data.index!==4){
    let index=this.data.index
    this.setData({
      index:index+1
    })
     backgroundAudioManager.src=this.data.historyList[this.data.index].url
     backgroundAudioManager.title=this.data.historyList[this.data.index].name
    }else{
      wx.showToast({
        title: '已经是最后一首',
        icon:'none'
      })
    }
  },

  onChangeLyricShow() {
    this.setData({
      isLyricShow: !this.data.isLyricShow
    })
  },

  timeUpdate(event) {
    this.selectComponent('.lyric').update(event.detail.currentTime)
  },

  onPlay() {
    this.setData({
      isPlaying: true,
    })
  },
  onPause() {
    this.setData({
      isPlaying: false,
    })
  },

  // 保存播放历史
  savePlayHistory() {


  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})